package com.biz.hunter.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.biz.hunter.db.entity.Domain;
import com.biz.hunter.db.entity.Employee;
import com.biz.hunter.dbutil.BaseDAO;

@Repository
public class DomainDAO {
	private static final String SQL_GET_ID = "select * from tb_domain  where id = ?";
	private static final String SQL_GET_NAME = "select * from tb_domain  where Name = ?";
	private static final String SQL_GET_ALL = "select * from tb_domain ";

	private static final String SQL_GET_ALL_SUBDOMAIN = "select * from tb_domain where level=1";
	private static final String SQL_GET_ALL_MEDICAL = "select * from tb_domain where parentId = 1";
	private static final String SQL_GET_ALL_DRUG = "select * from tb_domain where parentId = 2";

	public List<Domain> getALL() {

		return BaseDAO.queryForList(Domain.class, SQL_GET_ALL);

	}

	public List<Domain> getAllSubDomain() {
		return BaseDAO.queryForList(Domain.class, SQL_GET_ALL_SUBDOMAIN);
	}

	public List<Domain> getAllSubDomain(int id) {

		if (id == 1) {
			return getAllMedicalDomain();
		} else {
			return getAllDrugDomain();
		}
	}

	public List<Domain> getAllMedicalDomain() {
		return BaseDAO.queryForList(Domain.class, SQL_GET_ALL_MEDICAL);
	}

	public List<Domain> getAllDrugDomain() {
		return BaseDAO.queryForList(Domain.class, SQL_GET_ALL_DRUG);
	}

	public Domain getById(int Id) {
		return BaseDAO.queryForObject(Domain.class, SQL_GET_ID, Id);
	}

	public Domain getByName(String name) {
		return BaseDAO.queryForObject(Domain.class, SQL_GET_NAME, name);
	}

	public int getRootDomainId(int domainId) {
		if (domainId == 1 || domainId ==2) {
			return domainId;
		}

		Object parentId = BaseDAO.queryOneRowOneCol("select parentId from tb_domain where id = ?", domainId);
		if (parentId != null) {
			return ((Integer) parentId).intValue();
		}
		return -1;

	}

	public List<Domain> getByEmployee(Employee employee) {
		int domainId1 = employee.getMajorDomainId1();
		int domainId2 = employee.getMajorDomainId2();
		int domainId3 = employee.getMajorDomainId3();
		if (domainId1 > 0) {
			// todo :
		}
		String SQL_GET_EMP = "select * from tb_domain  where id in (?,?,?)";
		return BaseDAO.queryForList(Domain.class, SQL_GET_EMP, domainId1, domainId2, domainId3);

	}

}
